package com.fengniu.ejb.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.fengniu.ejb.business.IContentBusinessLocal;
import com.fengniu.ejb.business.IContentBusinessRemote;
import com.fengniu.ejb.dao.CategoryDaoLocal;
import com.fengniu.ejb.dao.ContentDaoLocal;
import com.fengniu.ejb.dao.SiteDaoLocal;
import com.fengniu.ejb.dao.UserDaoLocal;
import com.fengniu.ejb.dao.util.IEJBConstants;
import com.fengniu.ejb.entity.Category;
import com.fengniu.ejb.entity.Content;
import com.fengniu.ejb.entity.Content2Category;
import com.tianbing.adlottery.global.IConstants;

@Stateless
@LocalBinding(jndiBinding = IEJBConstants.ContentBusinessLocalJndiName)
@RemoteBinding(jndiBinding = IEJBConstants.ContentBusinessRemoteJndiName)
public class ContentBusinessImpl implements IContentBusinessLocal, IContentBusinessRemote {
	@EJB(name = IEJBConstants.UserDaoLocalJndiName)
	private UserDaoLocal userDao;

	@EJB(name = IEJBConstants.SiteDaoLocalJndiName)
	private SiteDaoLocal siteDao;

	@EJB(name = IEJBConstants.ContentDaoLocalJndiName)
	private ContentDaoLocal contentDao;

	@EJB(name = IEJBConstants.CategoryDaoLocalJndiName)
	private CategoryDaoLocal categoryDao;

	public Content2Category findContent2BrandCategory(long contentSysId) {
		Integer[] relationIds = { IConstants.RelationID.C2G_IS_BRAND_MEMBER_OF.getId() };
		List<Content2Category> c2gs = contentDao.getListContent2CategoryBy(contentSysId, relationIds, -1, null, null,
				0, 1, null);
		if (c2gs != null && c2gs.size() > 0)
			return c2gs.get(0);
		return null;
	}

	public List<Category> getContentLongestCategoryTrail(long contentSysId, String limitedTopCategoryId) {
		List<Category> result = null;
		// get site's cateogry trail
		List<List<Category>> trails = this.getContentCategoryTrails(contentSysId, limitedTopCategoryId);
		if (trails != null || trails.size() > 0) {
			for (List<Category> cats : trails) {
				if (result == null || result.size() < cats.size()) {
					result = cats;
				}
			}
		}
		return result;
	}

	public List<List<Category>> getContentCategoryTrails(long contentSysId, String limitedTopCategoryId) {
		List<List<Category>> categoryTrails = new ArrayList<List<Category>>(0);
		Integer[] relationIds = { IConstants.RelationID.C2G_IS_MEMBER_OF.getId() };
		List<Content2Category> c2gs = contentDao.getListContent2CategoryBy(contentSysId, relationIds, -1, null, null,
				0, 100, null);
		if (c2gs != null && c2gs.size() > 0) {
			for (int i = 0; i < c2gs.size(); i++) {
				Content2Category c2gi = c2gs.get(i);
				List<Category> trail = categoryDao.getListCategoryPrimaryTrailBy(c2gi.getCategory().getCategoryId());
				if (trail != null && trail.size() > 0) {
					Category topCat = trail.get(0);
					if (limitedTopCategoryId == null || limitedTopCategoryId.trim() == "") {
						categoryTrails.add(trail);
					} else if (topCat.getCategoryId() != null && topCat.getCategoryId().equals(limitedTopCategoryId)) {
						categoryTrails.add(trail);
					}
				}
			}
		}
		return categoryTrails;
	}

	private void rankingContent(long contentSysId, String limitedTopCategoryId, int maxCategoryMember) {
		Content content = contentDao.findContentBySysId(contentSysId);
		if (content != null) {
			List<Category> categoryList = this.getContentLongestCategoryTrail(contentSysId, null);
			Integer[] relationIds = { IConstants.RelationID.C2G_IS_MEMBER_OF.getId() };
			Integer[] contentTypeIds = { content.getTypeId() };
			if (categoryList != null && categoryList.size() > 0) {
				for (Category category : categoryList) {
					//1. we get the maxCategoryMember-nth content member of the category 
					List<Content2Category> nthMembers = contentDao.getListContent2CategoryBy(-1, relationIds, category
							.getSysId(), null, contentTypeIds, maxCategoryMember - 1, 1, null);
					List<Content2Category> theContent2CategoryList = null;
					if (nthMembers != null && nthMembers.size() > 0) {
						Content2Category nthMember = nthMembers.get(0);
						if (nthMember.getContent().getIntegral() != null
								&& nthMember.getContent().getIntegral().compareTo(
										content.getIntegral() == null ? 0 : content.getIntegral()) < 0) {
							// check whether the content has been the category's member
							theContent2CategoryList = contentDao.getListContent2CategoryBy(content.getSysId(),
									relationIds, category.getSysId(), null, contentTypeIds, 0, 1, null);
						}
					} else {
						theContent2CategoryList = contentDao.getListContent2CategoryBy(content.getSysId(), relationIds,
								category.getSysId(), null, contentTypeIds, 0, 1, null);
					}
					if (theContent2CategoryList == null || theContent2CategoryList.size() == 0) {
						Content2Category theContent2Category = new Content2Category();
						theContent2Category.setContent(content);
						theContent2Category.setRelationId(IConstants.RelationID.C2G_IS_MEMBER_OF.getId());
						theContent2Category.setCategory(category);
						contentDao.saveContent2Category(theContent2Category);
					}
				}
			}
			content.setLastRankedDate(new Date());
			contentDao.saveContent(content);
		}
	}

	public void rankingContent(long contentSysId, int maxCategoryMember) {
		this.rankingContent(contentSysId, IConstants.CategoryID.CONTENT_TOP.getId(), maxCategoryMember);
		this.rankingContent(contentSysId, IConstants.CategoryID.BRAND_TOP.getId(), maxCategoryMember);
	}

}
